{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Using default config.\n",
      "INFO:tensorflow:Using config: {'_save_checkpoints_secs': 600, '_num_ps_replicas': 0, '_keep_checkpoint_max': 5, '_tf_random_seed': None, '_task_type': None, '_environment': 'local', '_is_chief': True, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x6982ef30>, '_tf_config': gpu_options {\n",
      "  per_process_gpu_memory_fraction: 1\n",
      "}\n",
      ", '_task_id': 0, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_evaluation_master': '', '_keep_checkpoint_every_n_hours': 10000, '_master': ''}\n",
      "WARNING:tensorflow:From /usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/head.py:1362: scalar_summary (from tensorflow.python.ops.logging_ops) is deprecated and will be removed after 2016-11-30.\n",
      "Instructions for updating:\n",
      "Please switch to tf.summary.scalar. Note that tf.summary.scalar uses the node name instead of the tag. This means that TensorFlow will automatically de-duplicate summary names based on the scope they are created in. Also, passing a tensor or list of tags to a scalar summary op is no longer supported.\n",
      "INFO:tensorflow:Create CheckpointSaverHook.\n",
      "INFO:tensorflow:Saving checkpoints for 1 into /tmp/iris_model/model.ckpt.\n",
      "INFO:tensorflow:loss = 1.93453, step = 1\n",
      "INFO:tensorflow:global_step/sec: 115.28\n",
      "INFO:tensorflow:loss = 1.09608, step = 101\n",
      "INFO:tensorflow:global_step/sec: 128.192\n",
      "INFO:tensorflow:loss = 1.09607, step = 201\n",
      "INFO:tensorflow:global_step/sec: 128.54\n",
      "INFO:tensorflow:loss = 1.09607, step = 301\n",
      "INFO:tensorflow:global_step/sec: 132.66\n",
      "INFO:tensorflow:loss = 1.09607, step = 401\n",
      "INFO:tensorflow:global_step/sec: 129.689\n",
      "INFO:tensorflow:loss = 1.09607, step = 501\n",
      "INFO:tensorflow:global_step/sec: 127.194\n",
      "INFO:tensorflow:loss = 1.09607, step = 601\n",
      "INFO:tensorflow:global_step/sec: 127.367\n",
      "INFO:tensorflow:loss = 1.09607, step = 701\n",
      "INFO:tensorflow:global_step/sec: 127.626\n",
      "INFO:tensorflow:loss = 1.09607, step = 801\n",
      "INFO:tensorflow:global_step/sec: 124.069\n",
      "INFO:tensorflow:loss = 1.09607, step = 901\n",
      "INFO:tensorflow:global_step/sec: 129.075\n",
      "INFO:tensorflow:loss = 1.09607, step = 1001\n",
      "INFO:tensorflow:global_step/sec: 127.563\n",
      "INFO:tensorflow:loss = 1.09607, step = 1101\n",
      "INFO:tensorflow:global_step/sec: 128.849\n",
      "INFO:tensorflow:loss = 1.09607, step = 1201\n",
      "INFO:tensorflow:global_step/sec: 124.76\n",
      "INFO:tensorflow:loss = 1.09607, step = 1301\n",
      "INFO:tensorflow:global_step/sec: 128.621\n",
      "INFO:tensorflow:loss = 1.09607, step = 1401\n",
      "INFO:tensorflow:global_step/sec: 131.069\n",
      "INFO:tensorflow:loss = 1.09607, step = 1501\n",
      "INFO:tensorflow:global_step/sec: 125.608\n",
      "INFO:tensorflow:loss = 1.09607, step = 1601\n",
      "INFO:tensorflow:global_step/sec: 118.155\n",
      "INFO:tensorflow:loss = 1.09607, step = 1701\n",
      "INFO:tensorflow:global_step/sec: 128.63\n",
      "INFO:tensorflow:loss = 1.09607, step = 1801\n",
      "INFO:tensorflow:global_step/sec: 129.359\n",
      "INFO:tensorflow:loss = 1.09607, step = 1901\n",
      "INFO:tensorflow:Saving checkpoints for 2000 into /tmp/iris_model/model.ckpt.\n",
      "INFO:tensorflow:Loss for final step: 1.09607.\n",
      "WARNING:tensorflow:From /usr/local/lib/python2.7/dist-packages/tensorflow/contrib/learn/python/learn/estimators/head.py:1362: scalar_summary (from tensorflow.python.ops.logging_ops) is deprecated and will be removed after 2016-11-30.\n",
      "Instructions for updating:\n",
      "Please switch to tf.summary.scalar. Note that tf.summary.scalar uses the node name instead of the tag. This means that TensorFlow will automatically de-duplicate summary names based on the scope they are created in. Also, passing a tensor or list of tags to a scalar summary op is no longer supported.\n",
      "INFO:tensorflow:Starting evaluation at 2017-04-04-23:19:19\n",
      "INFO:tensorflow:Evaluation [1/1]\n",
      "INFO:tensorflow:Finished evaluation at 2017-04-04-23:19:23\n",
      "INFO:tensorflow:Saving dict for global step 2000: accuracy = 0.266667, auc = 0.399998, global_step = 2000, loss = 1.12176\n",
      "WARNING:tensorflow:Skipping summary for global_step, must be a float or np.float32.\n",
      "\n",
      "Test Accuracy: 0.266667\n",
      "\n",
      "New Samples, Class Predictions:    [0, 0]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from __future__ import absolute_import\n",
    "from __future__ import division\n",
    "from __future__ import print_function\n",
    "\n",
    "import os\n",
    "import urllib\n",
    "\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "\n",
    "# Data sets\n",
    "IRIS_TRAINING = \"iris_training.csv\"\n",
    "IRIS_TRAINING_URL = \"http://download.tensorflow.org/data/iris_training.csv\"\n",
    "\n",
    "IRIS_TEST = \"iris_test.csv\"\n",
    "IRIS_TEST_URL = \"http://download.tensorflow.org/data/iris_test.csv\"\n",
    "\n",
    "def main():\n",
    "  # If the training and test sets aren't stored locally, download them.\n",
    "  if not os.path.exists(IRIS_TRAINING):\n",
    "    raw = urllib.urlopen(IRIS_TRAINING_URL).read()\n",
    "    with open(IRIS_TRAINING, \"w\") as f:\n",
    "      f.write(raw)\n",
    "\n",
    "  if not os.path.exists(IRIS_TEST):\n",
    "    raw = urllib.urlopen(IRIS_TEST_URL).read()\n",
    "    with open(IRIS_TEST, \"w\") as f:\n",
    "      f.write(raw)\n",
    "\n",
    "  # Load datasets.\n",
    "  training_set = tf.contrib.learn.datasets.base.load_csv_with_header(\n",
    "      filename=IRIS_TRAINING,\n",
    "      target_dtype=np.int,\n",
    "      features_dtype=np.float32)\n",
    "  test_set = tf.contrib.learn.datasets.base.load_csv_with_header(\n",
    "      filename=IRIS_TEST,\n",
    "      target_dtype=np.int,\n",
    "      features_dtype=np.float32)\n",
    "\n",
    "  # Specify that all features have real-value data\n",
    "  feature_columns = [tf.contrib.layers.real_valued_column(\"\", dimension=4)]\n",
    "\n",
    "  # Build 3 layer DNN with 10, 20, 10 units respectively.\n",
    "  classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,\n",
    "                                              hidden_units=[10, 20, 10],\n",
    "                                              n_classes=3,\n",
    "                                              model_dir=\"/tmp/iris_model\")\n",
    "  # Define the training inputs\n",
    "  def get_train_inputs():\n",
    "    x = tf.constant(training_set.data)\n",
    "    y = tf.constant(training_set.target)\n",
    "\n",
    "    return x, y\n",
    "\n",
    "  # Fit model.\n",
    "  classifier.fit(input_fn=get_train_inputs, steps=2000)\n",
    "\n",
    "  # Define the test inputs\n",
    "  def get_test_inputs():\n",
    "    x = tf.constant(test_set.data)\n",
    "    y = tf.constant(test_set.target)\n",
    "\n",
    "    return x, y\n",
    "\n",
    "  # Evaluate accuracy.\n",
    "  accuracy_score = classifier.evaluate(input_fn=get_test_inputs,\n",
    "                                       steps=1)[\"accuracy\"]\n",
    "\n",
    "  print(\"\\nTest Accuracy: {0:f}\\n\".format(accuracy_score))\n",
    "\n",
    "  # Classify two new flower samples.\n",
    "  def new_samples():\n",
    "    return np.array(\n",
    "      [[6.4, 3.2, 4.5, 1.5],\n",
    "       [5.8, 3.1, 5.0, 1.7]], dtype=np.float32)\n",
    "\n",
    "  predictions = list(classifier.predict(input_fn=new_samples))\n",
    "\n",
    "  print(\n",
    "      \"New Samples, Class Predictions:    {}\\n\"\n",
    "      .format(predictions))\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
